package com.example.saveword.save_word;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.example.saveword.R;
import com.example.saveword.dao.StudyRecordDao;
import com.example.saveword.dao.WordDao;
import com.example.saveword.dao.WordRecordDao;
import com.example.saveword.model.StudyRecord;
import com.example.saveword.model.User;
import com.example.saveword.model.Word;
import com.example.saveword.model.WordRecord;
import com.example.saveword.utils.JsonUtils;
import com.example.saveword.utils.SPUtils;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends Fragment{

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private Button bt_study, bt_review, bt_modCnt;
    private AlertDialog alertDialog;
    private TextView tv_study, tv_review;
    private Context context;
    private User user;
    private WordDao wordDao;
    private StudyRecordDao studyRecordDao;
    private ProgressBar pb_home_bar;
    private TextView tv_home_pb;

    public HomeFragment(Context context) {
        // Required empty public constructor
        this.context = context;
        user = (User) JsonUtils.jsonToObject((String) SPUtils.get(context, "User", ""), User.class);
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment HomeFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static HomeFragment newInstance(String param1, String param2, Context context) {
        HomeFragment fragment = new HomeFragment(context);
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        bt_modCnt = view.findViewById(R.id.modifyWordCnt);
        bt_study = view.findViewById(R.id.bt_study);
        bt_review = view.findViewById(R.id.bt_review);
        tv_study = view.findViewById(R.id.tv_studyCnt);
        tv_review = view.findViewById(R.id.tv_reviewCnt);
        pb_home_bar = view.findViewById(R.id.pb_home_bar);
        tv_home_pb = view.findViewById(R.id.tv_home_pb);
        StudyRecordDao studyRecordDao = new StudyRecordDao(context);
        WordRecordDao wordRecordDao = new WordRecordDao(context);
        wordDao = new WordDao(context);
        StudyRecord studyRecord = studyRecordDao.addOrGet();
        tv_study.setText(String.valueOf(studyRecord.getNeedNewNum() - studyRecord.getNewNum()));
        tv_review.setText(String.valueOf(wordRecordDao.findNeedReview().size()));
        bt_study.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                StudyRecord studyRecord = studyRecordDao.addOrGet();
                if(studyRecord.getNeedNewNum() - studyRecord.getNewNum() == 0)
                {
                    @SuppressLint("DefaultLocale") AlertDialog alertDialog1 = new AlertDialog.Builder(context)
                            .setTitle("不需要背了!!!!")//标题
                            .setMessage("今天的任务已经完成了！")//内容
                            .setIcon(R.drawable.ic___)//图标
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {//添加"Yes"按钮
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                }
                            }).
                            setCancelable(false)
                            .create();
                    alertDialog1.show();
                    return ;
                }
                startActivity(new Intent(context, MainStudyActivity.class));
            }
        });
        bt_review.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(wordRecordDao.findNeedReview().size() == 0)
                {
                    @SuppressLint("DefaultLocale") AlertDialog alertDialog1 = new AlertDialog.Builder(context)
                            .setTitle("不需要复习了!!!!")//标题
                            .setMessage("今天的任务已经完成了！")//内容
                            .setIcon(R.drawable.ic___)//图标
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {//添加"Yes"按钮
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                }
                            }).
                            setCancelable(false)
                            .create();
                    alertDialog1.show();
                    return ;
                }
                startActivity(new Intent(context, MainReviewActivity.class));
            }
        });
        bt_modCnt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showSingleAlertDialog(view);
            }
        });
        // 修改进度条
        int id = user.get_id();
        int cet4WordAmount = wordDao.getTypeCount("CET4");
        int cet4WordLearningAmount = studyRecordDao.getLearningWordByIdAndType(String.valueOf(id), "CET4");
        String progress = cet4WordLearningAmount + "/" + cet4WordAmount;
        tv_home_pb.setText(progress);
        pb_home_bar.setMax(cet4WordAmount);
        pb_home_bar.setProgress(cet4WordLearningAmount);
        return view;
    }
    public void showSingleAlertDialog(View view){
        studyRecordDao = new StudyRecordDao(context);
        StudyRecord studyRecord = studyRecordDao.addOrGet();
        int org = studyRecord.getNeedNewNum();
        int checkId = 0;
        final String[] items = {"5", "10", "15", "20", "25", "30", "35", "40", "45"};
        for(int i = 0; i < 9; i++)
        {
            if(Integer.parseInt(items[i]) == org)
            {
                checkId = i;
                break;
            }
        }
        Log.d("773", String.valueOf(checkId));
        Log.d("773", String.valueOf(org));
        AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
        alertBuilder.setTitle("选择每日的词数");
        alertBuilder.setSingleChoiceItems(items, checkId, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                studyRecord.setNeedNewNum(Integer.parseInt(items[i]));
                studyRecordDao.update(studyRecord);
                tv_study.setText(String.valueOf(studyRecord.getNeedNewNum() - studyRecord.getNewNum()));
                Toast.makeText(context, items[i], Toast.LENGTH_SHORT).show();
            }
        });
        alertDialog = alertBuilder.create();
        alertDialog.show();
    }
    @Override
    public void onResume() {
        super.onResume();
        StudyRecordDao studyRecordDao = new StudyRecordDao(context);
        WordRecordDao wordRecordDao = new WordRecordDao(context);
        StudyRecord studyRecord = studyRecordDao.addOrGet();
        tv_study.setText(String.valueOf(studyRecord.getNeedNewNum() - studyRecord.getNewNum()));
        tv_review.setText(String.valueOf(wordRecordDao.findNeedReview().size()));
        int id = user.get_id();
        int cet4WordAmount = wordDao.getTypeCount("CET4");
        int cet4WordLearningAmount = studyRecordDao.getLearningWordByIdAndType(String.valueOf(id), "CET4");
        String progress = cet4WordLearningAmount + "/" + cet4WordAmount;
        tv_home_pb.setText(progress);
        pb_home_bar.setMax(cet4WordAmount);
        pb_home_bar.setProgress(cet4WordLearningAmount);
    }

}